//
//  PayResultViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/27.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
/// 支付结果页
class PayResultViewController: BaseViewController {
    
    var handlerClosure: (() -> Void)?
    
    private let orderType: OrderType
    private let viewModel: PublishViewModel
    private let fromOrder: Bool

    init(with vm: PublishViewModel, orderType: OrderType, fromOrder: Bool = false) {
        self.orderType = orderType
        self.viewModel = vm
        self.fromOrder = fromOrder
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var descLabel: UILabel!
    
    @IBOutlet private weak var okBtn: UIButton! {
        didSet {
            okBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
            okBtn.setBackgroundImage(UIImage(color: .mainColorGray), for: .highlighted)
            okBtn.layer.cornerRadius = okBtn.height * 0.5
            okBtn.layer.masksToBounds = true
        }
    }
}

// MARK: - Action
extension PayResultViewController {
    @IBAction private func okAction(_ sender: UIButton) {
        
        if orderType == .nurse{
            handlerClosure?()
            navigationController?.popToRootViewController(animated: true)
            return
        }
        
        if !fromOrder {
            NotificationCenter.default.post(name: NotiName.surePayResult, object: orderType)
            navigationController?.popToRootViewController(animated: true)
        } else {
            handlerClosure?()
            navigationController?.popViewController(animated: true)
        }
    }
}

// MARK: - 设置数据
extension PayResultViewController {
    private func setupUI() {
        switch orderType {
        case .advisory:
            setupAdvisoryUI()
        case .cloud:
            setupCloudUI()
        case .prescription:
            setupPrescriptionUI()
        case .nurse:
            setupNurseUI()
        }
    }
    
    private func setupAdvisoryUI() {
        descLabel.isHidden = true
        titleLabel.isHidden = false
        titleLabel.text = "您成功预约了\(viewModel.doctorName) \(viewModel.departmentName) \(viewModel.doctorOffice)的图文咨询"
    }
    
    private func setupNurseUI() {
        descLabel.isHidden = true
        titleLabel.isHidden = false
        titleLabel.text = "您成功预约了"
    }
    
    private func setupCloudUI() {
        descLabel.isHidden = false
        titleLabel.isHidden = false
        titleLabel.text = "您预约了\(viewModel.reserveTime) \(viewModel.scheduleTime) 的云门诊届时医生会与您图文+视频对话"
        descLabel.text = "请提前20分钟打开APP等候报道"
    }
    
    private func setupPrescriptionUI() {
        descLabel.isHidden = true
        titleLabel.isHidden = true
    }
}
